<?php

namespace app\data\controller\control;
use app\data\model\ControlClaimListModel;
use app\data\model\EnterStorehouseItemModel;
use think\admin\Controller;
use think\facade\Db;

/**
 * 公共数据管理
 * Class Cate
 * @package app\data\controller\shop
 */
class Lists extends Controller
{

    /**
     * 公共分类管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = "物资分类管理";
        if(input('types')==1){$query = $this->_query('SnControlMaterial');}
        if(input('types')==2){$query = $this->_query('SnControlAccessory');}
        // 加载对应数据
        $this->type = $this->request->get('type', 'index');
        if ($this->type === 'index') $query->where(['deleted' => 1]);
        elseif ($this->type === 'recycle') $query->where(['deleted' => 0]);
        else $this->error("无法加载 {$this->type} 数据列表！");
        // 列表排序并显示
        $query->whereRaw('char_length(material_code) = 13')->like('material_name,material_code,material_model');
        $query->equal('status')->order('material_code asc')->page();

    }


    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        foreach ($data as $k=>$v){
            $data[$k]['cate_id'] = 1;
        }
    }


    /**
     * 公共仓库数据管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function data_index()
    {
        $this->title = '仓库数据管理';
        $query = $this->_query('SnStorehouseData');
        $userId = session('user')['id'];
        //判断库存表是否存在
        $isTable = Db::query('SHOW TABLES LIKE '."'".'sn_enter_storehouse_'.$userId."'");
        if($isTable){
            //表存在
            $ksList = Db::name('sn_enter_storehouse_'.$userId)->column('material_num','material_code');
            $query->whereIn('material_code',array_keys($ksList));
            $query->like('material_code,material_name,material_model');
            $query->equal('status')->order('create_at desc')->page();
        }else{
            //表不存在
            $query->where('id',0)->order('create_at desc')->page();
        }
        // 列表排序并显示
        // $query->like('material_code,material_name,material_model');
        // $query->equal('status')->order('create_at desc')->page();
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _data_index_page_filter(array &$data)
    {
        $userId = session('user')['id'];
        if(empty($data)){
            $data = array();
        }else{
            $material_codes = array_column($data,'material_code');
            $order = 'field(material_code,'.implode(',',$material_codes).')';
            $material_nums = array_column($data,'material_num');
            //判断库存表是否存在
            $isTable = Db::query('SHOW TABLES LIKE '."'".'sn_enter_storehouse_'.$userId."'");
            if($isTable){
                $data = Db::name('sn_enter_storehouse_'.$userId)->whereIn('material_code',$material_codes)->order(Db::raw($order))->select()->toArray();
                foreach ($data as $k=>$v){
                    $data[$k]['material_xjptice'] = sprintf("%01.2f", $v['material_price'] * $v['material_num']);
                    $data[$k]['storehouse_num'] = $material_nums[$k];
                }
                $newData = array();
                foreach ($data as $k=>$v){
                    $newData[$v['material_code']] = $v;
                }
                //获取出库数据
                $ControlClaimListModel = new ControlClaimListModel();
                $claim_nums = $ControlClaimListModel->whereIn('status',[0,1])->whereIn('material_code',array_keys($newData))->group('material_code')->column('sum(material_num)','material_code');
                foreach ($newData as $ks=>$vs){
                    if(!isset($claim_nums[$ks])){
                        $newData[$ks]['claim_num'] = 0;
                        $newData[$ks]['surplus_num'] = $newData[$ks]['storehouse_num'];
                    }else{
                        $newData[$ks]['claim_num'] = $claim_nums[$ks];
                        $newData[$ks]['surplus_num'] =  sprintf("%01.2f", $newData[$ks]['storehouse_num'] - $claim_nums[$ks]);
                    }
                }
                //获取期初剩余数据
                $EnterStorehouseItemModel = new EnterStorehouseItemModel();
                foreach ($newData as $ks=>$vs){
                    if(isset($start_nums[$ks])){
                        $newData[$ks]['start_num'] = $start_nums[$ks]??0;
                        $newData[$ks]['surplus_num'] =  sprintf("%01.2f", $newData[$ks]['surplus_num'] + $start_nums[$ks]);
                    }
                }
                sort($newData);
                $data = $newData;
            }else{
                $data = array();
            }
         
        }
        
    }
    
    
    /**
     * 公共仓库领用数据管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cliam_data()
    {
        $this->title = '仓库数据管理';
        // 列表排序并显示
        $query = $this->_query('sn_enter_storehouse_item');
        $this->userId = session('user')['id'];
        //判断库存表是否存在
        $isTable = Db::query('SHOW TABLES LIKE '."'".'sn_enter_storehouse_'.$this->userId."'");
        if($isTable){
            //表存在
            $ksList = Db::name('sn_enter_storehouse_'.$this->userId)->order('material_code asc')->column('material_num','material_code');
            $query->like('material_code,material_name,material_model')->group('material_code')->field('*,sum(material_num) as material_num');
            $query->whereIn('material_code',array_keys($ksList));
            $query->equal('status')->order('material_code asc')->page();
        }else{
            //表不存在
            $query->where('id',0)->order('material_code asc')->page();
        }
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _cliam_data_page_filter(array &$data)
    { 
        if(empty($data)){
            $data = array();
        }else{
            $userId = session('user')['id'];
            $material_codes = array_column($data,'material_code');
            $order = 'field(material_code,'.implode(',',$material_codes).')';
            $material_nums = array_column($data,'material_num');
            //判断库存表是否存在
            $isTable = Db::query('SHOW TABLES LIKE '."'".'sn_enter_storehouse_'.$userId."'");
            if($isTable){
                $data = Db::name('sn_enter_storehouse_'.$userId)->whereIn('material_code',$material_codes)->order(Db::raw($order))->select()->toArray();
                foreach ($data as $k=>$v){
                    $data[$k]['material_xjptice'] = sprintf("%01.2f", $v['material_price'] * $v['material_num']);
                    $data[$k]['storehouse_num'] = $material_nums[$k];
                }
                $newData = array();
                foreach ($data as $k=>$v){
                    $newData[$v['material_code']] = $v;
                }
                //获取出库数据
                $ControlClaimListModel = new ControlClaimListModel();
                $claim_nums = $ControlClaimListModel->whereIn('status',[0,1])->whereIn('material_code',array_keys($newData))->group('material_code')->column('sum(material_num)','material_code');
                foreach ($newData as $ks=>$vs){
                    if(!isset($claim_nums[$ks])){
                        $newData[$ks]['claim_num'] = 0;
                        $newData[$ks]['surplus_num'] = $newData[$ks]['storehouse_num'];
                    }else{
                        $newData[$ks]['claim_num'] = $claim_nums[$ks];
                        $newData[$ks]['surplus_num'] =  sprintf("%01.2f", $newData[$ks]['storehouse_num'] - $claim_nums[$ks]);
                    }
                }
                sort($newData);
                $data = $newData;
            }else{
                $data = array();
            }
         
        }
        
    }


    /**
     * 公共仓库数据管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function ex_data_list()
    {
        $this->title = '仓库数据管理';
        // 列表排序并显示
        $query = $this->_query('sn_enter_storehouse_item');
        $userId = input('userId');
        //判断库存表是否存在
        $isTable = Db::query('SHOW TABLES LIKE '."'".'sn_enter_storehouse_'.$userId."'");
        if($isTable){
            //表存在
            $ksList = Db::name('sn_enter_storehouse_'.$userId)->order('material_code asc')->column('material_num','material_code');
            $query->like('material_code,material_name,material_model')->group('material_code')->field('*,sum(material_num) as material_num');
            $query->whereIn('material_code',array_keys($ksList));
            $query->equal('status')->order('material_code asc')->page();
        }else{
            //表不存在
            $query->where('id',0)->order('material_code asc')->page();
        }
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _ex_data_list_page_filter(array &$data)
    { 
        if(empty($data)){
            $data = array();
        }else{
            $material_codes = array_column($data,'material_code');
            $order = 'field(material_code,'.implode(',',$material_codes).')';
            $material_nums = array_column($data,'material_num');
            //判断库存表是否存在
            $isTable = Db::query('SHOW TABLES LIKE '."'".'sn_enter_storehouse_'.input('userId')."'");
            if($isTable){
                $data = Db::name('sn_enter_storehouse_'.input('userId'))->whereIn('material_code',$material_codes)->order(Db::raw($order))->select()->toArray();
                foreach ($data as $k=>$v){
                    $data[$k]['material_xjptice'] = sprintf("%01.2f", $v['material_price'] * $v['material_num']);
                    $data[$k]['storehouse_num'] = $material_nums[$k];
                }
                $newData = array();
                foreach ($data as $k=>$v){
                    $newData[$v['material_code']] = $v;
                }
                //获取出库数据
                $ControlClaimListModel = new ControlClaimListModel();
                $claim_nums = $ControlClaimListModel->whereIn('status',[0,1])->whereIn('material_code',array_keys($newData))->group('material_code')->column('sum(material_num)','material_code');
                foreach ($newData as $ks=>$vs){
                    if(!isset($claim_nums[$ks])){
                        $newData[$ks]['claim_num'] = 0;
                        $newData[$ks]['surplus_num'] = $newData[$ks]['storehouse_num'];
                    }else{
                        $newData[$ks]['claim_num'] = $claim_nums[$ks];
                        $newData[$ks]['surplus_num'] =  sprintf("%01.2f", $newData[$ks]['storehouse_num'] - $claim_nums[$ks]);
                    }
                }
                sort($newData);
                $data = $newData;
            }else{
                $data = array();
            }
         
        }
        
    }
}